--
-- Author:      feilong
-- DateTime:    2019-12-16 17:19:33
-- Description: 服务管理

local skynet = require "skynet"
local MessageDispatch = import("MessageDispatch")
local NodeMessage = import("NodeMessage")
local MessagePack = import("MessagePack")

local UserManager = import("gate.UserManager")
local LoginHandler = import("gate.LoginHandler")
local MessageHandler = import("gate.MessageHandler")
local SessionManager = import("gate.SessionManager")
local AccountHandler = import("gate.AccountHandler")

-- global = {} --服务只有一个全局变量，已在class.lua里声明
--
local function init()
	--用户数据
	local user_manager = UserManager.new()	
	local message_pack = MessagePack.new()
	--节点消息
	local node_message = NodeMessage.new(message_pack)

	--消息派发
	local message_dispatch = MessageDispatch.new()
	--会话管理
	local session_manager = SessionManager.new(message_dispatch)	
	--处理服务间消息
	local message_handler = MessageHandler.new(message_dispatch, node_message, session_manager, user_manager)
	--登陆处理
	local login_handler = LoginHandler.new(message_dispatch)
	local account_handler = AccountHandler.new(message_dispatch)
	
	global.message_pack = message_pack

	global.session_manager = session_manager
	global.user_manager = user_manager
	global.node_message = node_message
	global.message_dispatch = message_dispatch
	global.message_handler = message_handler
	global.login_handler = login_handler
	global.account_handler = account_handler

	--socket消息
	skynet.register_protocol {
		name = "client",
		id = skynet.PTYPE_CLIENT,
		--消息解包
		unpack = handler(message_pack, message_pack.unpackClient),
		--从客户发来的消息
		dispatch = handler(message_handler, message_handler.dispatchClientMessage),		
	}
	--处理服务消息
	skynet.dispatch("lua", message_dispatch:dispatch())

end


-----------------------------------------------------------------------------------------
--skyent
-----------------------------------------------------------------------------------------

skynet.start(function()

	init()

end)